package com.thebox.util;

import org.apache.velocity.app.Velocity;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.Template;

import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

import org.jdom.*; 
import org.jdom.input.*; 

import java.io.*;
import java.util.*;

public class VeBean {
    public void go(String templateFile, String xf) {
        try {
            //Velocity.init("velocity.properties");
            Velocity.init();
            
            VelocityContext context = new VelocityContext();
            setContext(context, xf);

            Template template =  null;

            try {
                template = Velocity.getTemplate(templateFile);
            } catch (ResourceNotFoundException rnfe) {
                System.out.println("Example : error : cannot find template " + templateFile );
            } catch( ParseErrorException pee ) {
                System.out.println("Example : Syntax error in template " + templateFile + ":" + pee );
            }

            BufferedWriter writer = writer = new BufferedWriter(
                new OutputStreamWriter(System.out));

            if ( template != null)
                template.merge(context, writer);

            /*
             *  flush and cleanup
             */

            writer.flush();
            writer.close();
        } catch( Exception e ) {
            System.out.println(e);
        }
    }

    private void setContext(VelocityContext context, String xf) throws Exception {
        SAXBuilder builder = new SAXBuilder(false); 
		Document doc = builder.build(new FileInputStream(xf)); 

		Element root = doc.getRootElement(); 
        Element tmp = root.getChild("package");
        context.put("package", tmp.getText());
        
        tmp = root.getChild("imports");
        List importList = new ArrayList();
        for (Iterator it = tmp.getChildren("import").iterator(); it.hasNext(); ) {
            Element ch = (Element)it.next();
            importList.add(ch.getText());
        }
        context.put("importList", importList);
        
        tmp = root.getChild("class");
        context.put("className", tmp.getChildText("name"));
        context.put("baseName", tmp.getChildText("extends"));
        
        tmp = root.getChild("fields");
        List fieldList = new ArrayList();
        for (Iterator it = tmp.getChildren("field").iterator(); it.hasNext(); ) {
            Element ch = (Element)it.next();
            Field f = new Field(ch.getText(), ch.getAttributeValue("type"));
            fieldList.add(f);
        }
        context.put("fieldList", fieldList);
    }            
        
    public static void main(String[] args) {
        if (args.length < 2) {
            print("java VeBean template xml");
            return;
        }
        VeBean t = new VeBean();
        t.go(args[0], args[1]);
    }
    
    public static void print(String str) {
        System.out.println(str);
    }
        
    public class Field {
        String name = null;
        String type = null;
        
        public Field(String n, String t) {
            name = n;
            type = t;
        }
        
        public String getName() {
            return name;
        }
        
        public String getType() {
            return type;
        }
        
        public String getFuncName() {
            return name.substring(0, 1).toUpperCase() + name.substring(1);
        }
    }
}
